#include "parserimpl.h"
#include "cjsrootimpl.h"
#include "json.h"
#include <io/iframe.h>

CParserImpl::CParserImpl(void)
{
	
}

CParserImpl::~CParserImpl(void)
{

}
//////////////////////////////////////////////////////////////////////////
std_method_impl CParserImpl::init_class(IBase* prot, IBase* punkOuter)
{
	if (prot != NULL)
	{
		m_pRot	=	prot;
	}
	return S_OK;
}
std_method_impl CParserImpl::Init(void*)
{
	
	return S_OK;
}
std_method_impl CParserImpl::Uninit()
{

	return S_OK;
}
std_method_impl CParserImpl::Start()
{

	return S_OK;
}
std_method_impl CParserImpl::Stop()
{
	return S_OK;
}
//////////////////////////////////////////////////////////////////////////

std_method_impl CParserImpl::NewObject(const char* data, IBase** ioNodeInfo)
{
	JsRoot*	pObject = NULL;
	pObject = new CJsRootImpl();
	pObject->LoadData((char*)data);
	pObject->QueryInterface(_uuidof(IBase), (void**)ioNodeInfo);
	return S_OK;
}
std_method_impl CParserImpl::NewLoadFileObject(const TCHAR* filepath, IBase** ioNodeInfo)
{

	HRESULT hr = S_OK;

	retrtpv(filepath, E_FAIL);
	retrtpv(ioNodeInfo, E_FAIL);
	CComLoader			comLoader;
	_lComPtr<IAsynIo>	pAsynIo		= INULL;
	_lComPtr<IReadFile>	pReadFile	= INULL;

	if (m_pRot != INULL)
	{
		m_pRot->GetObject(CLSID_IAsynFrame, _uuidof(IAsynIo), (IBase**)&pAsynIo.m_p);
	}

	if (pAsynIo != NULL)
	{
		pAsynIo->CreateReadFile((IBase**)&pReadFile.m_p);
	}
	else
	{

		tstring path = GetCurrentRunPath(NULL);

		tstring asynio;
		asynio.append(path);

		asynio += ModNameToModPath(_T("asynio"));

		retrtpv(FileIsExist(asynio.c_str()), E_FAIL)

		BOOL bRet = FALSE;
		bRet = comLoader.LoadCom(asynio.c_str());
		if (bRet == FALSE)
		{
			return E_FAIL;
		}
		hr = comLoader.CreateInstance(CLSID_IAsynFrame, NULL, NULL, _uuidof(IAsynIo), (void**)&pAsynIo.m_p);

		if (hr == S_OK)
		{
			pAsynIo->CreateReadFile((IBase**)&pReadFile.m_p);
		}
		else
		{
			return E_FAIL;
		}
	}
	retrtpv(pReadFile.m_p, E_FAIL)
	retrtpv(FileIsExist(filepath), E_FAIL)

	hr = pReadFile->LoadFile(filepath, 0, 0, 0, 0);

	os_int64 ulFileSize = pReadFile->GetFileSize();

	retrtpv(ulFileSize, E_FAIL)

	sentry<char*, default_array_sentry> pBuffer;

	size_t filesize = ulFileSize & UINT32_MAX;

	pBuffer  = Memory_Allocate char[filesize + 1];
	os_int64 ulReadLen = -1;
	pReadFile->ReadBuffer(pBuffer, filesize, &ulReadLen);
	pReadFile->CloseFile();

	JsRoot*	pObject = new CJsRootImpl();
	pObject->LoadData((char*)pBuffer.m_p);
	pObject->QueryInterface(_uuidof(IBase),(void**)ioNodeInfo);

	return S_OK;
}
std_method_impl CParserImpl::DelObject(IBase* ioNodeInfo)
{

	if (ioNodeInfo != INULL)
	{
		ioNodeInfo->Release();
	}
	return S_OK;
}


std_method_impl CParserImpl::JsonToXML(const char* input, char*  output, unsigned long ulen)
{

	string strxml = CJson::Json2Xml(input);

	if (strxml.length() <= ulen)
		string_strcpy(output, strxml.c_str());
	else
		return E_FAIL;
	

	return S_OK;
}
std_method_impl CParserImpl::XMLToJson(const char* input, char*  output, unsigned long ulen)
{

	string strjson = CJson::Json2Xml(input);

	if (strjson.length() <= ulen)
		string_strcpy(output, strjson.c_str());
	else
		return E_FAIL;

	return S_OK;
}
